package DataBaseQuery;

import java.util.ArrayList;

import DataBaseManager.Row;

public class Group {
	Row groupedFieldRows;
	ArrayList<VariableReference> fields;
	ArrayList<Row> rows;
	
	public Group(Row group,ArrayList<VariableReference> f)
	{
		groupedFieldRows = group;
		fields = f;
		rows = new ArrayList<Row>();
	}
	
	public RegularView toView(ArrayList<VariableReference> groupedFields)
	{
		RegularView v= new RegularView();
		v.fields.addAll(groupedFields);
		v.fields.addAll(fields);
		for (Row row : rows) {
			Row newRow = new Row();
			newRow.addAll(groupedFieldRows.getValues());
			newRow.addAll(row.getValues());
			v.getRows().add(newRow);
		}
		for (int i = 0; i < groupedFields.size(); i++) {
			v.fields.get(i).setValue(groupedFieldRows.getValue(i));
		}
		return v;
	}
	
	public boolean isInGroup(Row group)
	{
		for(int i=0;i<groupedFieldRows.size();i++)
		{
			if(!group.getValue(i).isEqual(groupedFieldRows.getValue(i)))
			{
				return false;
			}
		}
		return true;
	}
}
